const util = require("../../../utils/util");
const api = require("../../../config/api");
// pages/report/report_core/report_core.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    fileSort: 1, //项目的提交参数
    filePath: []  //存储远程文件路径
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let detail = JSON.parse(options.p)
    this.setData({
      detail
    })
    if (detail.fileVoList) {
      let that = this;
      for (let i = 0; i < detail.fileVoList.length; i++) {
        // console.log(i);
        const fileId = detail.fileVoList[i].id;
        const fileSort = this.data.fileSort;
        util.request(api.downloadFile, {fileId, fileSort}, 'GET').then(res => {
          console.log(res.data.path);
          const path = res.data.path
          //下载文件 讲路径转换为本地临时路径可以打开
          wx.downloadFile({
            url: path,
            header: {
              'Content-Type': 'application/json',
              'Authorization': wx.getStorageSync('token')
            },
            success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                console.log(res);
                let filePathObj = {}
                filePathObj.path = res.tempFilePath
                that.data.filePath.push(filePathObj)
                that.setData({
                  filePath: that.data.filePath
                })
              }
  
            }
          })
        })

      }
    }
  },
  //打开文件
  openDoc(e) {
    let i = e.currentTarget.dataset.index;
    let filePath = this.data.filePath[i].path;

    console.log(e);
    //打开文件
    wx.openDocument({
      filePath,
      success: function (res) {
        // console.log(res);
      },
      fail(err) {
        console.log(err);
      }
    })
  }
})